from querier.esquerier import ElasticSearchQuerier
from querier.wechat.content_analyze import WechatContentAnalyzeQuerier


class WechatContentAnalyzeAllQuerier(ElasticSearchQuerier):
    def __init__(self, es, index, doc_type):
        super(WechatContentAnalyzeAllQuerier, self).__init__(es, index, doc_type)
        self.analyzeQuerier = WechatContentAnalyzeQuerier(es, index, doc_type)

    def search(self, args):
        by_fields = args.get('by')
        biz_code = args.get('biz_code', None)
        from_date = args.get('from', None)
        to_date = args.get('to', None)

        if biz_code is None:
            raise ValueError('"biz_code" is needed.')

        if by_fields is None:
            by_fields = ['category', 'content_type']
        res = {}
        for value in by_fields:
            a = {"biz_code": biz_code, "by_field": value, "from": from_date, "to": to_date}
            res['category' if value == 'category' else value] = self.analyzeQuerier.search(a)

        return res

    def _build_query(self, args): pass

    def _build_result(self, es_result, param): pass
